package vn.vietel.pms.web.action;

import java.util.List;

import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;

import vn.vietel.pms.entity.VGroupOfProperty;
import vn.vietel.pms.service.GroupOfPropertyService;

@Scope(ScopeType.SESSION)
@Name("groupOfProperty")
public class GroupOfPropertyAction {

	@In(required = false)
	@Out(required = false)
	@DataModelSelection("listGroups")
	private VGroupOfProperty vgroupofproperty;

	private VGroupOfProperty beingEdit;
	
	@In("#{groupOfPropertyService}")
	private GroupOfPropertyService groupOfPropertyService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VGroupOfProperty> listGroups;

	private Long id;

	private boolean editCompleted;

	private boolean editing;
	
	private boolean editingGroup;
	
	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		editing = false;
		editCompleted = false;
		this.id = id;
		if (id != null) {
			vgroupofproperty = groupOfPropertyService.findGroup(id);
			editing = (vgroupofproperty != null);
		} else {
			vgroupofproperty = new VGroupOfProperty();
			editing = false;
		}
	}

	public void createItem() {
		try {
			editCompleted = false;
			groupOfPropertyService.createGroup(vgroupofproperty);
			listGroups.add(vgroupofproperty);
			editCompleted = true;
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editCompleted = false;
		}
	}

	public void updateItem() {
		editCompleted = false;
		try {
			groupOfPropertyService.updateGroup(vgroupofproperty);
			for(VGroupOfProperty gr:listGroups){
				if(gr.getId().longValue() == vgroupofproperty.getId().longValue()){
				   gr.setGroupName(vgroupofproperty.getGroupName());
				   gr.setGroupDesc(vgroupofproperty.getGroupDesc());
				}
			}
			editCompleted = true;
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
			editCompleted = false;
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = vgroupofproperty.getId();
			listGroups.remove(vgroupofproperty);
			VGroupOfProperty rgp = groupOfPropertyService.findGroup(rId);
			groupOfPropertyService.deleteGroup(rgp);
			listGroups.remove(vgroupofproperty);
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void selectItem(ActionEvent ae) {
		try {
			Long rId = vgroupofproperty.getId();
			System.out.println("Edit item: "+rId);
			beingEdit = groupOfPropertyService.findGroup(rId);
			vgroupofproperty = beingEdit;
			System.out.println("Edit item: "+vgroupofproperty.getGroupName());
			setEditingGroup(true);
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}
	@Factory()
	public List<VGroupOfProperty> getListGroups() {
		listGroups = groupOfPropertyService.getAllGroups();
		return listGroups;
	}

	public void setListGroups(List<VGroupOfProperty> listGroups) {
		this.listGroups = listGroups;
	}

	public boolean isEditCompleted() {
		return editCompleted;
	}

	public void setEditCompleted(boolean created) {
		this.editCompleted = created;
	}


	public boolean isEditing() {
		return editing;
	}


	public void setEditing(boolean editing) {
		this.editing = editing;
	}

	public boolean getNotEditing(){
		return !editing;
	}


	public void setEditingGroup(boolean editingGroup) {
		this.editingGroup = editingGroup;
	}


	public boolean isEditingGroup() {
		return editingGroup;
	}


	public void setBeingEdit(VGroupOfProperty beingEdit) {
		this.beingEdit = beingEdit;
	}


	public VGroupOfProperty getBeingEdit() {
		return beingEdit;
	}
}
